package com.example.teaching_server.service;

import com.example.teaching_server.dto.CourseRecordingDTO;
import com.example.teaching_server.entity.CourseRecording;
import com.example.teaching_server.repository.CourseRecordingRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程录制服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CourseRecordingService {

    private final CourseRecordingRepository recordingRepository;

    /**
     * 获取课程录播记录
     */
    public List<CourseRecordingDTO> getAllRecordings(String className, LocalDate startDate, LocalDate endDate) {
        log.info("查询课程录播记录: className={}, startDate={}, endDate={}", className, startDate, endDate);

        LocalDateTime startDateTime = startDate != null ? startDate.atStartOfDay() : null;
        LocalDateTime endDateTime = endDate != null ? endDate.atTime(LocalTime.MAX) : null;

        List<CourseRecording> recordings = recordingRepository.findRecordings(className, startDateTime, endDateTime);

        return recordings.stream()
                .map(CourseRecordingDTO::fromEntity)
                .collect(Collectors.toList());
    }
}
